package com.aitesting.testcase;

import com.aitesting.constant.AssertEnum;
import com.aitesting.domain.IStep;
import com.aitesting.domain.TStep;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 填充断言数据
 */
public class StepWithValidation implements IStep {
    private final TStep step;

    public StepWithValidation(TStep step) {
        this.step = step;
    }

    /**
     * 填充断言数据
     * @param json_path json路径
     * @param expected_value 期望结果
     * @param message 断言名称
     * @return packageAssertData对象
     */
    private static List<Object> packageAssertData(String json_path, Object expected_value, String message){
        List<Object> data = new ArrayList<>();
        data.add(json_path);
        data.add(expected_value);
        data.add(message);
        return data;
    }

    /**
     * 相等断言
     * @param json_path json路径
     * @param expected_value 期望结果
     * @param message 断言名称
     * @return packageAssertData对象
     */
    public StepWithValidation equal(String json_path, Object expected_value, String message){
        List<Object> data = packageAssertData(json_path, expected_value, message);
        Map<String, Object> map = new HashMap<>();
        map.put(AssertEnum.equal.getName(), data);
        this.step.getValidator().add(map);
        return this;
    }

    /**
     * 不相等断言
     * @param json_path json路径
     * @param expected_value 期望结果
     * @param message 断言名称
     * @return packageAssertData对象
     */
    public StepWithValidation not_equal(String json_path, Object expected_value, String message){
        List<Object> data = packageAssertData(json_path, expected_value, message);
        Map<String, Object> map = new HashMap<>();
        map.put(AssertEnum.notEqual.getName(), data);
        this.step.getValidator().add(map);
        return this;
    }

    /**
     * 包含断言
     * @param json_path json路径
     * @param expected_value 期望结果
     * @param message 断言名称
     * @return packageAssertData对象
     */
    public StepWithValidation contains(String json_path, Object expected_value, String message){
        List<Object> data = packageAssertData(json_path, expected_value, message);
        Map<String, Object> map = new HashMap<>();
        map.put(AssertEnum.contains.getName(), data);
        this.step.getValidator().add(map);
        return this;
    }

    /**
     * 提取接口返回参数
     * @return StepRequestExtraction对象
     */
    public StepRequestExtraction extract(){
        return new StepRequestExtraction(step);
    }

    @Override
    public TStep perform() {
        return this.step;
    }
}
